package org.lex.swing.table;

import javax.swing.JTable;

public class AutoResizeUtils {
	public static final String COLUMNS = "autoResize.columns";
	public static final String PREFIX = "autoResize.column.";

	public static void reportWidth(JTable table, int modelIndex, int width) {
		String key = PREFIX + modelIndex;
		Object max = table.getClientProperty(key);
		if (null == max) {
			table.putClientProperty(key, width);
		} else {
			if (width > (Integer) max)
				table.putClientProperty(key, width);
		}
	}

	public static int getColumnWidth(JTable table, int modelIndex) {
		Object src = table.getClientProperty(PREFIX + modelIndex);
		if (null == src)
			return -1;
		return (Integer) src;
	}

	public static void resetAllColumnWidth(JTable table, int defaultWidth) {
		int columns = table.getModel().getColumnCount();
		for (int i = 0; i < columns; i++)
			table.putClientProperty(PREFIX + i, defaultWidth);
	}
}
